import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class NameServlet extends HttpServlet {

 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 String name = (String) req.getSession().getAttribute("visitor");
 if (name == null) {
 name = req.getParameter("name");
 if (name == null) {
 resp.getWriter().println(
 "<html><body><form>Hello! What is your name? " +
 "<input type=\"text\" name=\"name\"/>" +
 "<input type=\"submit\" value=\"OK\"/>" +
 "</form></body></html>");
 return;
 }
 req.getSession().setAttribute("visitor", name);
 }
 resp.getWriter().println("Hello " + name + "!");
 }

}